<?php

namespace app\admin\logic;

use app\common\model\mysql\CategoryModel;
use app\common\model\mysql\LabelModel;
use app\common\model\mysql\ProjectModel;
use app\common\model\mysql\UserLevelModel;
use app\common\service\UploadService;
use app\common\logic\BaseLogic;

class ProjectLogic extends BaseLogic
{
    public function index($request)
    {
        $search = $request->param();
        $category = CategoryModel::getList(['status' => 1]);
        $where = [];

        if (!empty($search['name'])) {
            $where[] = ['name', 'LIKE', '%' . $search['name'] . '%'];
        }
        if (!empty($search['category_id'])) {
            $where[] = ['category_id', '=', $search['category_id']];
        } else {
            $search['category_id'] = '';
        }
        if ($request->account['project_ids']) {
            $where[] = ['id', 'in', $request->account['project_ids']];
        }
        $list = ProjectModel::getPaginate($where, [], 50, ['sort' => 'ASC', 'id' => 'desc'], ["query" => $search]);

        return view('Project/project', compact('list', 'search', 'category'));
    }

    public function create($request)
    {
        $category = CategoryModel::getList(['status' => '1'], ['id', 'name'], [], ['sort' => 'ASC']);

        $label = LabelModel::getList(['status' => '1'], ['id', 'name'], [], ['sort' => 'ASC']);

        $level = UserLevelModel::getList([], ['id', 'name'], [], ['id' => 'ASC']);

        return view('Project/project_add', compact('category', 'label', 'level'));
    }

    public function save($request)
    {
        $param = $request->param();
        $inputData = [
            'name' => $param['name'],
            'category_id' => $param['category_id'],
            'label_ids' => empty($param['label_ids']) ? '' : implode(',', $param['label_ids']),
            'sort' => $param['sort'],
            'integral' => $param['integral'],
            'describe' => $param['describe'],
            'detail' => $param['detail'],
            'order_require' => $param['order_require'],
            'new_status' => $param['new_status'],
            'type' => $param['type'],
            'status' => $param['status'],
            'num' => $param['num'],
            'at_level' => $param['at_level'],
        ];
        // 缩略图
        if ($request->has('project_thumb.name', 'file', true)) {
            $inputData['thumb'] = UploadService::getInstance()->uploadForm('project_thumb', 'project/thumb');
        }

        // 视频上传
        if (!empty($param['video'])) {
            $inputData['video'] = $param['video'];
        }


        try {
            ProjectModel::addData($inputData);
        } catch (\Exception $e) {

            return $this->responseError('保存失败');
        }

        $redirectUrl = config('admin.app_host') . '/admin/project';

        return $this->responseSuccess('保存成功', ['redirect_url' => $redirectUrl]);
    }

    public function edit($request, $id)
    {
        $info = ProjectModel::getOne(['id' => $id]);

        $category = CategoryModel::getList(['status' => '1'], ['id', 'name'], [], ['sort' => 'ASC']);

        $label = LabelModel::getList(['status' => '1'], ['id', 'name'], [], ['sort' => 'ASC']);

        $level = UserLevelModel::getList([], ['id', 'name'], [], ['id' => 'ASC']);

        return view('Project/project_edit', compact('category', 'info', 'label', 'level'));
    }

    public function update($request, $id)
    {
        $param = $request->param();
        $inputData = [
            'name' => $param['name'],
            'category_id' => $param['category_id'],
            'label_ids' => empty($param['label_ids']) ? '' : implode(',', $param['label_ids']),
            'sort' => $param['sort'],
            'integral' => $param['integral'],
            'describe' => $param['describe'],
            'detail' => $param['detail'],
            'new_status' => $param['new_status'],
            'order_require' => $param['order_require'],
            'type' => $param['type'],
            'status' => $param['status'],
            'num' => $param['num'],
            'at_level' => $param['at_level'],
        ];
        // 缩略图
        if ($request->has('project_thumb.name', 'file', true)) {
            $inputData['thumb'] = UploadService::getInstance()->uploadForm('project_thumb', 'project/thumb');
        }
        // 视频上传
        if (!empty($param['video'])) {
            $inputData['video'] = $param['video'];
        }
        try {
            ProjectModel::editData($inputData, ['id' => $id]);
        } catch (\Exception $e) {
            return $this->responseError('保存失败');
        }
        return $this->responseSuccess('保存成功');
    }

    public function renew($request, $id)
    {
        $param = $request->param();
        $inputData = [];
        isset($param['sort']) ? $inputData['sort'] = $param['sort'] : false;
        isset($param['type']) ? $inputData['type'] = $param['type'] : false;
        isset($param['new_status']) ? $inputData['new_status'] = $param['new_status'] : false;
        try {
            ProjectModel::editData($inputData, ['id' => $id]);
        } catch (\Exception $e) {
            return $this->responseError('保存失败');
        }
        return $this->responseSuccess('保存成功');
    }

}